<?php
/**
 * Created by PhpStorm.
 * User: yzh
 * Date: 2019/5/11
 * Time: 17:05
 */

namespace app\api\controller\v1;


use app\api\service\Token;
use app\api\service\Poster as PosterService;
use app\api\controller\BaseController;
use app\lib\exception\UserException;

class Poster extends BaseController
{
    /**
     * 获取海报列表
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function  getPoster(){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $result =  $poster->getMaterial();
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90010;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取素材列表
     * @param $style
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPosterByClass($class,$size=10,$page){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $result =  $poster->getMaterialByClass($class,$size,$page);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90030;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取每日推荐名片
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRecommendPoster(){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $result = $poster->getRecommendPoster();
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90040;
            $data['content'] = '';
        }
        return json_encode($data);
    }


    /**
     * 获取二维码
     * @param $m_id
     * @param $style
     * @param $path
     * @param $words
     * @return \think\response\Json
     * @throws \app\lib\exception\MiniSuiteException
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getQRCode($m_id,$style,$path,$words){
        $wu_id  = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('t_id');
        $poster = new PosterService();
        $url = $poster->getQRCode($t_id,$wu_id,$m_id,$style,$path,$words);
        if (null != $url){
            $data['code'] = 200;
            $data['content'] = $url;
        }else{
            $data['code'] = 90020;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 添加海报
     * @param $id
     * @return \think\response\Json
     * @throws \app\lib\exception\MiniSuiteException
     */
    public function addPoster($id,$poster_id){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $url = $poster->addPoster($id,$poster_id);
        if ( $url){
            $data['code'] = 200;
            $data['content'] = 'success';
        }else{
            $data['code'] = 90050;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取海报信息
     * @param $id
     * @return \think\response\Json
     * @throws UserException
     * @throws \think\exception\DbException
     */
    public function getPosterInfo($id){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $url = $poster->getPosterByID($id);
         if (empty($url)){
            throw  new  UserException([
                'code'=>200,
                'msg'=>'获取海报不存在',
                'code'=>95060
            ]);
        }
        return json($url);
    }

    /**
     * 获取海报列表
     * @param $size
     * @param $page
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function getPosterList($size,$page){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $result = $poster->getPosterList($wu_id,$size,$page);
        if ( $result){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90070;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取总用户
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllUser(){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $result = $poster->getAllUser($wu_id);
        if ( $result){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90080;
            $data['content'] = '';
        }
        return json($data);
    }

    /**
     * 获取海报用户
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUser($id){
        $wu_id  = Token::getCurrentUid();
        $poster = new PosterService();
        $result = $poster->getUser($id);
        if ( $result){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 90080;
            $data['content'] = '';
        }
        return json($data);
    }






}